package com.example.recycleview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerAdapter recyclerAdapter;
    private List<Person> personList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        //点击长按滑动
        onclick();

    }
    //点击长按滑动
    private void onclick() {
        ItemTouchHelper.Callback callback=new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                //监控上下左右
                int swipFlag = 0;//ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT
                int dragflag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                return makeMovementFlags(dragflag,swipFlag);
            }

            //移动的处理
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                recyclerAdapter.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
                Collections.swap(personList,viewHolder.getAdapterPosition(),target.getAdapterPosition());
                return true;
            }
            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                //主要是做左右拖动的回调
            }
            @Override
            public boolean canDropOver(RecyclerView recyclerView, RecyclerView.ViewHolder current, RecyclerView.ViewHolder target) {
                //当前item可以被拖动到目标位置后,直接落到target上,后面的item也接着落
                return true;
            }
            @Override
            public boolean isLongPressDragEnabled() {
                //是否开启长按拖动
                //返回true  可以实现长按拖动排序和拖动动画
                return true;
            }
        };
        //创建item helper
        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(callback);
        //绑定到recyclerView上面
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

    //添加数据
    private void initData() {
        personList=new ArrayList<>();
        personList.add(new Person(1,"anmin","敲代码"));
        personList.add(new Person(2,"张三","出差"));
        personList.add(new Person(3,"join","吃饭"));
        personList.add(new Person(4,"李四","睡觉"));
        personList.add(new Person(5,"jack","打豆豆"));
    }
    //初始化
    private void initView(){
        recyclerView = findViewById(R.id.recycleview);
        //recyclerView.addItemDecoration(new DividerItemDecorations());
        recyclerView.addItemDecoration(new DividerItemDecorations(MainActivity.this, LinearLayoutManager.VERTICAL));
        // 线性布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        //适配器
        recyclerAdapter = new RecyclerAdapter(personList);
        recyclerView.setAdapter(recyclerAdapter);
    }
}